package com.yueyang.sojourn.server.controller.wx;

import com.yueyang.sojourn.server.common.LoginNeedless;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.enums.ShareDataTypeEnum;
import com.yueyang.sojourn.server.model.po.MemberMerch;
import com.yueyang.sojourn.server.model.po.SojournOrder;
import com.yueyang.sojourn.server.service.member.MemberMerchService;
import com.yueyang.sojourn.server.service.share.ShareDataService;
import com.yueyang.sojourn.server.service.sojourn.order.MemberMerchOrderService;
import com.yueyang.uc.model.UcUserV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/wx/memberMerch/")
@Api(tags = "(小程序)购买会员信息 API")
@RestController
public class ApiMemberMerchController extends BaseController {
    @Autowired
    private MemberMerchService memberMerchService;
    @Autowired
    private MemberMerchOrderService memberMerchOrderService;
    @Autowired
    private ShareDataService shareDataService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberMerchId", paramType = "query", required = true),
            @ApiImplicitParam(name = "X-Token", paramType = "header", required = false)
    })
    @ApiOperation(value = "购买会员的订单,返回订单id")
    @PostMapping("/order")
    public Long createOrder(@RequestHeader(value = "shareId", required = false) Long shareId,
                            @RequestParam("memberMerchId") Integer memberMerchId) {
        UcUserV1 loginUser = getLoginUser();
        SojournOrder order = memberMerchOrderService.createMemberOrder(loginUser, memberMerchId);
        if (shareId != null) {
            shareDataService.action(shareId, ShareDataTypeEnum.下单, getLoginUserId(), "购买了会员", order.getId());
        }
        return order.getId();
    }

    /**
     * 获取可购买的会员详情
     * @return
     */
    @GetMapping("/default")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-Token", paramType = "header", required = false)
    })
    @ApiOperation(value = "查询默认的会员商品信息")
    @LoginNeedless
    public MemberMerch findDefaultDetail() {
        return memberMerchService.findDefault();
    }


}
